package com.gurid.infrastructure.apigateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
//import org.springframework.web.servlet.config.annotation.CorsRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration //如果Controller上没有CrossOrigin注解，则使该类生效以使能跨域
public class CorsConfig /*implements WebMvcConfigurer*/ {

    /*@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*") // 允许跨域的域名，*代表任何域名
                .allowedMethods("GET", "POST", "DELETE", "PUT") // 允许的方法
                .allowedHeaders("*") // 允许任何请求头
                .allowCredentials(true) // 允许cookies
                *//*.exposedHeaders(HttpHeaders.of("SET_COOKIE",fie))*//*.maxAge(3600L);
    }*/

    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        //config.setAllowCredentials(true);
        config.addAllowedOriginPattern("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*"); //跨域的方法

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}
